package org.hmh.leetcode.array;

public class code714_medium {
    static class Solution {
        public int maxProfit(int[] prices, int fee) {
            if(prices.length<2){
                return 0;
            }
            int l= prices[0];
            int h = prices[0];
            int sum = 0;
            for(int i = 1; i < prices.length; i++) {
                if(prices[i]>h) h = prices[i];
                else if(h-prices[i]>fee){
                    //判断是否值得交易
                    if(h-l>fee){
                        sum+=(h-l-fee);
                    }
                    //重新计算
                    h = prices[i];
                    l = prices[i];
                }else if(prices[i]<l){
                    //重新计算
                    h = prices[i];
                    l = prices[i];
                }
            }
            if(h-l>fee) sum += (h-l - fee);
            return sum;
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        int[] arr = new int[]{1,3,2,8,4,9};
        System.out.println(solution.maxProfit(arr,2));
    }
}
